package pcb.scorm
{
	/**
	 * ...
	 * @author Fintan Boyle
	 */
	import pipwerks.SCORM;
	import flash.external.ExternalInterface;
	import pcb.scorm.Course;
	import com.intel.utils.Debugger;

	public class SCORMRuntime 
	{
		public static var LESSON_INCOMPLETE:String = "incomplete";
		public static var LESSON_COMPLETED:String = "completed";
		public static var LESSON_PASSED:String = "passed";
		
		private static var instance:SCORMRuntime;
		private var scormOb:SCORM;
		private var course:Course;
		
		private var _console:String = "";
		
		public function SCORMRuntime() 
		{
			init()
		}
		
		public static function getInstance():SCORMRuntime
		{
			if (!instance) {
				instance = new SCORMRuntime();
				
			}

			return instance;
		}
		
		private function init():void 
		{
			scormOb = new SCORM();  //Initialize a new SCORM object
			Debugger.traceOut("*************************************");
			Debugger.traceOut("Initializing a new SCORM object...");						
			course = new Course();
		}
		
		public function connect():void {
			
			var success:Boolean = isConnected();
			
			Debugger.traceOut("Connecting to SCORM runtime...");

			if(success){
				Debugger.traceOut("\tsuccess: " + success);
				//Set course variables
				course.lesson_status = getLessonStatus();
				
				//If course has already been completed
				if(course.lesson_status == SCORMRuntime.LESSON_PASSED || course.lesson_status == SCORMRuntime.LESSON_COMPLETED){
					
					//You have already completed this course;
					console = "You have already completed this course";
					//Disconnect from the LMS.
					disconnect();
					
				} else {
					
					//Set course status to incomplete
					success = setLessonStatus(SCORMRuntime.LESSON_INCOMPLETE);
					
					updateSessionTime();
					
					scormOb.set("cmi.core.session_time", course.session_time);
					
					Debugger.traceOut("Saving SCORM data...");
					if(success){
						save();
					} 
					Debugger.traceOut("\tsuccess: " + success);
					
				}		

			} else {
				Debugger.traceOut("success: " + success);
			}
			
			Debugger.traceOut("*************************************");
		}
		
		public function setLessonComplete():Boolean {
			if(course.lesson_status == SCORMRuntime.LESSON_PASSED || course.lesson_status == SCORMRuntime.LESSON_COMPLETED){
				
				//You have already completed this course;
				console = "You have already completed this course";
				
			} else {
				
				//Set course status to complete
				var success:Boolean = setLessonStatus(SCORMRuntime.LESSON_COMPLETED);
				updateSessionTime();
				Debugger.traceOut("Saving SCORM data...");
				if(success){
					save();
				} 
				Debugger.traceOut("\tsuccess: " + success);
				
			}	
			return true;
		}
		
		
		public function setLessonStatus(status:String):Boolean 
		{
			var boo:Boolean = scormOb.set("cmi.core.lesson_status", status)
			if (boo) {
				course.lesson_status = status;
				return true;
			}
			return false;
		}
		
		private function updateSessionTime():Boolean 
		{
			var date:Date = new Date();
			var time:String = date.getHours() + ":" + date.getMinutes() + ":00.00";
				
			//needs to be in this format "01:50:23.00"
			var boo:Boolean = scormOb.set("cmi.core.session_time", time)
			if (boo) {
				course.session_time = time
				return true;
			}
			return false;
		}
		
		public function getLessonStatus():String 
		{
			return scormOb.get("cmi.core.lesson_status");
		}
		
		public function disconnect():Boolean 
		{
			return scormOb.disconnect();
		}
		
		public function isConnected():Boolean
		{
			return scormOb.connect();
		}
		
		public function save():void
		{
			scormOb.save();
		}
		
		
		public function get console():String { return _console; }
		
		public function set console(value:String):void 
		{
			_console += " >  "+ value + "\n";
		}
		
		
	}
}